<template>
  <div>
    <h1>新建文章</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="文章标题">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="文章图片">
        <el-upload
          class="avatar-uploader"
          :action="$http.defaults.baseURL + '/upload'"
          :headers="getAuthHeaders()"
          :show-file-list="false"
          :on-success="afterUpload"
        >
          <img v-if="form.url" :src="form.url" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="mp3">
     
      </el-form-item>

      <el-form-item label="文章详情">
        <vue-editor
          v-model="form.body"
          use-custom-image-handler
          @image-added="handleImageAdded"
        />
      </el-form-item>
      <el-button @click="save()">创建</el-button>
    </el-form>
  </div>
</template>

<script>
import { VueEditor } from "vue2-editor";
export default {
  data() {
    return {
      content: "",
      categories: [],
      form: {},
    };
  },
  created() {
    // this.getList();
  },
  components: { VueEditor },
  methods: {
    //   副文本编辑器  用formdata上传
    async handleImageAdded(file, Editor, cursorLocation, resetUploader) {
      console.log("123");
      const formData = new FormData();
      formData.append("file", file);
      const res = await this.$http.post("upload", formData);
      Editor.insertEmbed(cursorLocation, "image", res.data.url);
      resetUploader();
    },
    //   提交上传
    async save() {
      const res = await this.$http.post("/rest/newes", this.form);
      console.log(res);
    },
    afterUpload(res) {
      console.log(res);
      // 显示赋值 vue提供的 明确告诉vue 我们要在form上添加一个icon属性
      this.$set(this.form, "icon", res.url);
      this.form.url = res.url;
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>